<?php
App::uses('AppController', 'Controller');

/**
 * Items Controller
 *
 * @property Item $Item
 */
class ItemsController extends AppController {
	public function beforeFilter() {
		parent::beforeFilter();
	}
	
	public function admin_home() {	
	}
	
	public function admin_index($category_id = null, $album_id = null) {
		$conditions = array();
		if(isset($category_id) && $category_id > 0)
			$conditions['Category.id'] = $category_id;
			
		$categories = $this->Item->Category->find('all', array(
			'fields' => array('Category.id', 'Category.name'),
			'conditions' => $conditions,
			'order' => array('Category.name' => 'ASC')
		));

		$conditions = array();
		if(isset($album_id) && $album_id > 0)
			$conditions['Album.id'] = $album_id;
		$albums = $this->Item->Album->find('all', array(
			'fields' => array('Album.id', 'Album.name'),
			'conditions' => $conditions,
			'order' => array('Album.created' => 'DESC')
		));
		
		$this->set('categories', $categories);
		$this->set('albums', $albums);
		$this->set('category_id', $category_id);
		$this->set('album_id', $album_id);
	}
	
	public function admin_view() {	
	}
	
	public function admin_edit($id = null) {
		$this->Item->id = $id;
		if (!$this->Item->exists()) {
			throw new NotFoundException('Invalid item');
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Item->save($this->request->data)) {
				$this->Session->setFlash('The item has been saved');
				$url = $this->Session->read('Controller.toUrl');
				$this->redirect($url);
			} else {
				$this->Session->setFlash('The item could not be saved. Please, try again.');
			}
		} else {
			$options = array('conditions' => array('Item.' . $this->Item->primaryKey => $id));
			$this->request->data = $this->Item->find('first', $options);
			
			$categories = $this->Item->Category->find('all', array(
				'fields' => array('Category.id', 'Category.name'),
				'conditions' => array('Category.parent_id >' => 0),
				'order' => array('Category.name' => 'ASC')
			));
			$this->set('categories', $categories);
				
			$albums = $this->Item->Album->find('all', array(
				'fields' => array('Album.id', 'Album.name'),
				'order' => array('Album.created' => 'DESC')
			));
			$this->set('albums', $albums);
			
		}
	}	

	public function ajax_index($category_id = null, $album_id = null){
		// if category_id or album_id param appears, will select only in those ID
		// otherwise select from all categories and albums
		$conditions = array();
		if(isset($category_id) && $category_id > 0)
			$conditions['Item.category_id'] = $category_id;
		if(isset($album_id) && $album_id > 0)
			$conditions['Item.album_id'] = $album_id;
		
		$this->paginate = array(
			'recursive' => 0,
			'conditions' => $conditions,
			'fields'	=> array('Item.id', 'Item.album_id', 'Item.category_id', 
			'Item.name', 'Item.status', 'Item.stock', 'Item.created', 
			'Item.modified', 'Category.name', 'Album.name', 'Image.url1'),
			'limit' => 10,
			'group' => 'Item.id',
			'order' => array('Item.created' => 'DESC'),
			'joins' => array(
				array('table' => 'images',
					'alias' => 'Image',
					'type'	=> 'LEFT',
					'conditions' => array('Item.id = Image.item_id')
				)
			),
		);
		$this->set('items', $this->paginate());
	}
		
	public function api_add() {
		$error = -1;
		if ($this->request->is('post')) {
			$this->Item->create();
			if ($this->Item->save($this->request->data)) {
				$error = 0;
				$message = 'The item has been saved';
				if(!empty($this->request->data['Attach'])){
					$photos = $this->request->data['Attach'];
					foreach($photos as $photo){
						list($url1, $url2) = split("\|", $photo, 2);
						$this->Item->Image->create();
						$this->Item->Image->save(array(
							'item_id' => $this->Item->id,
							'url1' => $url1,
							'url2' => $url2,
						));
					}
				}
			} else {
				$error = 1;
				$message = 'The item could not be saved. Please, try again.';
			}
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
	
	public function api_delete($id = null) {
		$error = -1;
		$this->Item->id = $id;
		if (!$this->Item->exists()) {
			$message = 'Invalid item';
		}
		if ($this->Item->delete()) {
			$error = 0;
			$message = 'Item deleted';
		}
		else{
			$error = 1;
			$message = 'Item was not deleted';			
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
	
	public function api_status($id = null){
		$error = -1;
		$this->Item->id = $id;
		if (!$this->Item->exists()) {
			$message = 'Invalid item';
		}
		else{		
			if($this->Item->updateAll(
			array('Item.status' => '1 - Item.status', 'modified' => 'NOW()'),
			array('Item.id' => $id))){
				$error = 0;
				$message = 'Status has been changed';
			}
			else{
				$error = 1;
				$message = 'Status could not be changed';					
			}
		}
		$this->set('error', $error);
		$this->set('data', $message);		
	}
	
	public function api_stock($id = null){
		$error = -1;
		$this->Item->id = $id;
		if (!$this->Item->exists()) {
			$message = 'Invalid item';
		}
		else{		
			if($this->Item->updateAll(
			array('Item.stock' => '1 - Item.stock', 'modified' => 'NOW()'),
			array('Item.id' => $id))){
				$error = 0;
				$message = 'Stock has been changed';
			}
			else{
				$error = 1;
				$message = 'Stock could not be changed';					
			}
		}
		$this->set('error', $error);
		$this->set('data', $message);		
	}
}
?>